package fr.efrei.tradesys.hibernate.beans;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import fr.efrei.tradesys.treatments.Formatter;

@Entity
@Table(name = "RATE")
@SequenceGenerator(name = "S_RATE", sequenceName = "S_RATE")
public class Rate
{

	private int				idRate;
	private Currency	currency1;
	private Currency	currency2;
	private double		ask;
	private double		bid;
	private double		evolution;
	private Date			rateDate;

	public Rate()
	{
	}

	public Rate(double ask, double bid, double evolution, Date rateDate)
	{
		this.ask = ask;
		this.bid = bid;
		this.evolution = evolution;
		this.rateDate = rateDate;
	}

	public Rate(Currency currency1, Currency currency2, double ask, double bid,
			double evolution, Date rateDate)
	{
		this.currency1 = currency1;
		this.currency2 = currency2;
		this.ask = ask;
		this.bid = bid;
		this.evolution = evolution;
		this.rateDate = rateDate;
	}

	@Id
	@Column(name = "IDRATE", unique = true, nullable = false, precision = 6, scale = 0)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_RATE")
	public int getIdRate()
	{
		return idRate;
	}

	public void setIdRate(int idRate)
	{
		this.idRate = idRate;
	}

	@ManyToOne
	@JoinColumn(name = "CODE1", nullable = false)
	public Currency getCurrency1()
	{
		return currency1;
	}

	public void setCurrency1(Currency currency1)
	{
		this.currency1 = currency1;
	}

	@ManyToOne
	@JoinColumn(name = "CODE2", nullable = false)
	public Currency getCurrency2()
	{
		return currency2;
	}

	public void setCurrency2(Currency currency2)
	{
		this.currency2 = currency2;
	}

	@Column(name = "ASK", precision = 8, scale = 4, columnDefinition = "numeric", nullable = false)
	public double getAsk()
	{
		return ask;
	}

	public void setAsk(double ask)
	{
		this.ask = ask;
	}

	@Transient
	public String getAskFormatted()
	{
		return new Formatter().format(getAsk(), 4);
	}

	@Transient
	public double getAskReciprocal()
	{
		return 1 / getAsk();
	}

	@Transient
	public String getAskReciprocalFormatted()
	{
		return new Formatter().format(getAskReciprocal(), 4);
	}

	@Column(name = "BID", precision = 8, scale = 4, columnDefinition = "numeric", nullable = false)
	public double getBid()
	{
		return bid;
	}

	public void setBid(double bid)
	{
		this.bid = bid;
	}

	@Transient
	public String getBidFormatted()
	{
		return new Formatter().format(getBid(), 4);
	}

	@Transient
	public double getBidReciprocal()
	{
		return 1 / getBid();
	}

	@Transient
	public String getBidReciprocalFormatted()
	{
		return new Formatter().format(getBidReciprocal(), 4);
	}

	@Column(name = "EVOLUTION", precision = 6, scale = 2, columnDefinition = "numeric", nullable = false)
	public double getEvolution()
	{
		return evolution;
	}

	public void setEvolution(double evolution)
	{
		this.evolution = evolution;
	}

	@Transient
	public String getEvolutionFormatted()
	{
		return new Formatter().format(getEvolution(), 2);
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "RATEDATE", nullable = false)
	public Date getRateDate()
	{
		return rateDate;
	}

	public void setRateDate(Date rateDate)
	{
		this.rateDate = rateDate;
	}

	@Transient
	public String getRateDateFormatted()
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		return dateFormat.format(getRateDate());
	}

}
